package ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.domain.model.Nic;
import ch.zbw.pra.sysventory.domain.model.OperatingSystem;
import ch.zbw.pra.sysventory.domain.model.Printer;
import ch.zbw.pra.sysventory.domain.model.Processor;
import ch.zbw.pra.sysventory.domain.model.Software;
import ch.zbw.pra.sysventory.domain.model.VendorSoftware;
import ch.zbw.pra.sysventory.domain.visualisation.IVisuControllObserver;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControll;
import ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels.SearchPanel;
import ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels.SearchResultPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class OverViewAndSearchPanel extends JPanel implements IVisuControllObserver, ActionListener {

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OverViewAndSearchPanel.class);
	private boolean TEST_DATA = false;
	private static final long serialVersionUID = 6424472747589307251L;
	private SearchPanel searchpanel;
	private SearchResultPanel searchresultpanel;
	private List<Computer> currentComputerList;

	public OverViewAndSearchPanel() {

		this.searchresultpanel = new SearchResultPanel();
		this.searchpanel = new SearchPanel();
		if (this.TEST_DATA) {
			this.searchresultpanel.setComputerList(createTestData());
		} else {
			this.searchresultpanel.setComputerList(new ArrayList<Computer>());
		}
		this.searchpanel.addActionStartSearchButtonListener(this);
		init();
		addObservers(this);
	}

	public void init() {
		this.currentComputerList = new ArrayList<Computer>();
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagLayout.columnWeights = new double[] { 1.0 };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0 };
		int x = 0;
		int y = 0;

		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y++, this.searchpanel);
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x, y++, this.searchresultpanel);
	}

	private List<Computer> createTestData() {
		List<Computer> computers = new ArrayList<Computer>();
		Nic nic = new Nic("test", "122.12.12.12", "23:FF:AA:CC", "EE:DD:12:34:54", "10.0.1.5", "true", "10.20.20.21");
		List<Nic> nics = new ArrayList<Nic>();
		nics.add(nic);

		VendorSoftware vendor = new VendorSoftware("Adobe");
		Software software = new Software("Adobe Photoshop", "2435.4", vendor);
		List<Software> softwares = new ArrayList<Software>();
		softwares.add(software);
		softwares.add(new Software("Adobe Elements", "1234", vendor));

		Processor processor = new Processor("i7", "sandy-Bridge", 4, 3700);

		OperatingSystem os = new OperatingSystem("Windows XP", "34.3455", "64Bit");

		Printer printer = new Printer("HP Deskjet", "HP", "34.44");
		List<Printer> printers = new ArrayList<Printer>();
		printers.add(printer);

		Computer computer = new Computer("maetsbook", "435345", "4096", "Notebook", os, processor, softwares, nics, printers);
		computers.add(computer);
		return computers;
	}

	public void doStartSearch() {
		String computer = this.searchpanel.getTextComputerField();
		String ip = this.searchpanel.getTextSearchIpField();
		String computertyp = this.searchpanel.getTextSearchCompTyp();
		String processor = this.searchpanel.getTextProzField();
		String os = this.searchpanel.getTextOsField();
		this.currentComputerList = DomainController.getInstance().startSearchPC(computer, ip, computertyp, processor, os);
		this.searchresultpanel.setComputerList(this.currentComputerList);
	}

	public void update() {
		this.searchresultpanel.setComputerList(this.currentComputerList);
	}

	private void addObservers(IVisuControllObserver observer) {
		if (observer != null) {
			VisuControll.getInstance().getComputer().addObserver(observer);
			VisuControll.getInstance().getNic().addObserver(observer);
			VisuControll.getInstance().getOs().addObserver(observer);
			VisuControll.getInstance().getPrinter().addObserver(observer);
			VisuControll.getInstance().getProcessor().addObserver(observer);
			VisuControll.getInstance().getSoftware().addObserver(observer);
		}

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			doStartSearch();
		}
	}

}
